home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / cujaug93.zip / 1108110A < prev    next >
Text File  |  1993-06-09  |  683b  |  33 lines

  1. /* arith.c: Illustrate pointer arithmetic */
  2. #include <stdio.h>
  3. #include <stddef.h>
  4.  
  5. main()
  6. {
  7.     float a[] = {1.0, 2.0, 3.0};
  8.     float *p;
  9.     ptrdiff_t diff;
  10.  
  11.     /* Increment a pointer */
  12.     printf("sizeof(float) == %u\n",sizeof(float));
  13.     p = &a[0];
  14.     printf("p == %p, *p == %f\n",p,*p);
  15.     ++p;
  16.     printf("p == %p, *p == %f\n",p,*p);
  17.  
  18.     /* Subtract two pointers */
  19.     diff = (p+1) - p;
  20.     printf("diff == %d\n",diff);
  21.     diff = (char *)(p+1) - (char *)p;
  22.     printf("diff == %ld\n",(long)diff);
  23.     return 0;
  24. }
  25.  
  26. /* OUTPUT:
  27.  * sizeof(float) == 4
  28.  * p == FFEA, *p == 1.000000
  29.  * p == FFEE, *p == 2.000000
  30.  * diff == 1
  31.  * diff == 4 */
  32.  
  33.